#include "xfilter.h"

#include <QMutex>
#include "ximagepro.h"

class CXFilter : public XFilter{
public:
    std::vector<XTask> tasks;
    QMutex mutex;

    CXFilter(){}

    cv::Mat fFilter(cv::Mat mat1, cv::Mat mat2) {
        mutex.lock();
        XImagePro p;
        p.set(mat1, mat2);
        for (int i = 0; i < (int)tasks.size(); ++i) {
            switch (tasks[i].type) {
            case XTASK_GAIN:
                //亮度对比度调整
                p.gain(tasks[i].para[0], tasks[i].para[1]);
                break;
            case XTASK_ROTATE90:
                //旋转90
                p.rotate90();
                break;
            case XTASK_ROTATE180:
                //旋转180
                p.rotate180();
                break;
            case XTASK_ROTATE270:
                //旋转270
                p.rotate270();
                break;
            case XTASK_FLIPX:
                //上下镜像
                p.flipX();
                break;
            case XTASK_FLIPY:
                //左右镜像
                p.flipY();
                break;
            case XTASK_FLIPXY:
                //上下左右镜像
                p.flipXY();
                break;
            case XTASK_RESIZE:
                //尺寸调整
                p.resize(tasks[i].para[0], tasks[i].para[1]);
                break;
            case XTASK_PYDOWN:
                //高斯金字塔
                p.pyDown(tasks[i].para[0]);
                break;
            case XTASK_PYUP:
                //拉普拉斯金字塔
                p.pyUp(tasks[i].para[0]);
                break;
            case XTASK_CLIP:
                //裁剪
                p.clip(tasks[i].para[0], tasks[i].para[1], tasks[i].para[2], tasks[i].para[3]);
                break;
            case XTASK_GRAY:
                //灰度化
                p.gray();
                break;
            case XTASK_MARK:
                //添加水印
                p.mark(tasks[i].para[0], tasks[i].para[1], tasks[i].para[2]);
                break;
            case XTASK_BLEND:
                //视频融合
                p.blend(tasks[i].para[0]);
                break;
            case XTASK_MERGE:
                //视频合并
                p.merge();
                break;
            default:
                break;
            }
        }
        cv::Mat re = p.get();
        mutex.unlock();
        return re;
    }
    void fAdd(XTask task) {
        mutex.lock();
        tasks.push_back(task);
        mutex.unlock();
    }
    void fClear() {
        mutex.lock();
        tasks.clear();
        mutex.unlock();
    }

};

XFilter *XFilter::getInstance()
{
    static CXFilter cx;
    return &cx;
}

XFilter::~XFilter()
{

}

XFilter::XFilter()
{

}
